package com.longagoewk.springboot.controller.manageController;

import com.longagoewk.springboot.entity.ExplainBean;
import com.longagoewk.springboot.entity.UserBean;
import com.longagoewk.springboot.service.CommonService;
import com.longagoewk.springboot.service.ExplainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Controller
public class M_explainController {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    ExplainService explainService;

    @Autowired
    CommonService commonService;

    @RequestMapping("/addexplain")
    public ModelAndView addExplain(){
        ModelAndView view = new ModelAndView();
        UserBean userBean = (UserBean) request.getSession().getAttribute("user");
        ExplainBean explainBean = new ExplainBean(0,userBean.getmID(),"所在展厅","展厅名称","/image/explain/test1.jpg","");
        view.addObject("flag",0);
        view.addObject("explainBean",explainBean);
        view.setViewName("manage/M_changeExplain");
        return view;
    }

    @RequestMapping("/M_explain")
    public ModelAndView mExplain(){
        ModelAndView view = new ModelAndView();
        List<ExplainBean> list = getExplainList();
        view.addObject("list",list);
        view.setViewName("manage/M_explain");
        return view;
    }

    @RequestMapping("/selectExplain")
    public ModelAndView SelectExplain(String keyword) {
        ModelAndView view = new ModelAndView();
        List<ExplainBean> newlist = new ArrayList<>();
        List<ExplainBean> list = getExplainList();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getEname().contains(keyword)) {
                newlist.add(list.get(i));
            }
        }
        view.addObject("list",newlist);
        view.setViewName("manage/M_explain");
        return view;
    }

    @RequestMapping("/editExplain")
    public ModelAndView editExplain(String explains){
        ModelAndView view = new ModelAndView();
        String explainlist[] = explains.split(",");
        ExplainBean explainBean = new ExplainBean();
        explainBean.seteID(Integer.parseInt(explainlist[0]));
        explainBean = explainService.getExplain(explainBean);
        view.addObject("flag",1);
        view.addObject("explainBean",explainBean);
        view.setViewName("manage/M_changeExplain");
        return view;
    }


    @RequestMapping("/updataExplain")
    public ModelAndView updataExplain(ExplainBean explainBean,@RequestParam(value = "explainImage") MultipartFile imgFile,@RequestParam(value = "explainVoice") MultipartFile voiceFile)throws Exception{
        ModelAndView view = new ModelAndView();
        int flag = 0;
        List<MultipartFile> fileList = new ArrayList<>();
        String filemsg[][] = new String[2][2];
        String folder = "explain";
        fileList.add(imgFile);
        fileList.add(voiceFile);
        for (int i = 0; i < fileList.size(); i++) {
            filemsg[i][0] = fileList.get(i).getOriginalFilename();
            if (!filemsg[i][0].equals("")) {

                if (i==0){
                    filemsg[i][1] = "/image";
                }else{
                    filemsg[i][1] = "/audio";
                    filemsg[i][0] = "_f-" + filemsg[i][0];
                }
                filemsg[i][0] = UUID.randomUUID() + filemsg[i][0];
                commonService.UploadPictures(fileList.get(i), filemsg[i][1], folder, filemsg[i][0]);
                if(i==0){
                    explainBean.setEimage(filemsg[i][1]+"/explain/" + filemsg[i][0]);
                }else{
                    explainBean.setEvoice(filemsg[i][1]+"/explain/" + filemsg[i][0]);
                }
            }
        }
        if(explainBean.geteID()==0){
            if (explainService.addExplain(explainBean))
                flag = 1;
        }else{
            if (explainService.updataExplain(explainBean))
                flag = 1;
        }
        if(flag==1){
            List<ExplainBean> list = getExplainList();
            view.addObject("list",list);
        }else {
            view.addObject("list",null);
        }
        view.setViewName("manage/M_explain");
        return view;
    }

    @RequestMapping("/deleteExplains")
    public ModelAndView deleteExplains(String explainsList){
        ModelAndView view = new ModelAndView();
        String[] explains = explainsList.split(",");
        boolean flag = false;
        ExplainBean explainBean = new ExplainBean();
        for (String explainID : explains) {
            explainBean.seteID(Integer.parseInt(explainID));
            if (explainService.deleteExplain(explainBean)) {
                flag = true;
            }
        }
        List<ExplainBean> list = getExplainList();
        view.addObject("list",list);
        view.setViewName("manage/M_explain");
        if (flag) {
            return view;
        } else {
            return null;
        }

    }

    public List<ExplainBean> getExplainList(){
        UserBean userBean = (UserBean) request.getSession().getAttribute("user");
        ExplainBean explainBean = new ExplainBean();
        explainBean.setmID(userBean.getmID());
        List<ExplainBean> list = explainService.getExplainList(explainBean);
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getEvoice().equals("")) continue;
            String temp[] = list.get(i).getEvoice().split("_f-");
            list.get(i).setEvoice(temp[1]);
        }
        return list;
    }


}
